package fi.saarinen.simulator.instructions;

/**
 * Some bit-twiddling methods.
 * 
 * @author jsaarinen
 */
public class Util
{
  /**
   * Returns the integer value between two given bit positions.
   * 
   * @param bits
   * @param x1
   * @param x2
   * @return
   */
  public static int extractBits(int bits, int x1, int x2)
  {
    if (x2 >= x1)
    {
      throw new IllegalArgumentException();
    }
    // start from zero
    bits >>= x2;
    x1 >>= x2;
    int mask = 0;
    for (int i = 0; i < x1; i++)
    {
      mask <<= 1;
      mask |= 1;
    }
    return bits & mask;
  }
}
